package com.faithbj.danaliverpool.web.controller.admin;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.faithbj.danaliverpool.service.TemplateService;

/**
 * @author jrn E-mail: rinanjin@gmail.com
 * @version 0.1 
 * 创建时间：2012-7-6 下午11:41:11
 * 类说明
 */
@Controller
@RequestMapping(value = "faith/template/")
public class AdminTemplateController extends BaseAdminController
{
	private static final long serialVersionUID = 1L;

	private final String LIST = "/admin/template_list";
	
	private final String INPUT = "/admin/template_input";
	
	private final String CREATE = "/admin/template_create";
	
	@Resource
	private TemplateService templateService;
	
	@RequestMapping(value = "list")
	public String list(ModelMap map)
	{
		List<File> files = templateService.list();
		map.put("files", files);
		return LIST;
	}
	
	@RequestMapping(value = "add")
	public String add(ModelMap map)
	{
		map.put("isAddAction", true);
		
		return CREATE;
	}
	
	@RequestMapping(value = "create")
	public String create(@RequestParam("fileName") String fileName, ModelMap map)
	{
		templateService.create(fileName);
		
		return SUCCESS;
	}
	
	@RequestMapping(value = "edit")
	public String edit(@RequestParam("fileName") String fileName, ModelMap map)
	{
		String content = templateService.content(fileName);
		
		map.put("fileName", fileName);
		map.put("content", content);
		
		return INPUT;
	}
	
	@RequestMapping(value = "update")
	public String update(@RequestParam("fileName") String fileName, @RequestParam("content") String content, ModelMap map)
	{
		templateService.save(fileName, content);
		
		return SUCCESS;
	}

	@RequestMapping(value = "delete")
	public String delete(@RequestParam("fileName") String fileName, ModelMap map)
	{
		templateService.delete(fileName);
		
		return SUCCESS;
	}
}
